package com.googlecode.cnasort.cc.rmi.common;

import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 *
 */
public abstract class ClientRmiStarter<T extends Remote> extends RmiStarter<T>
{
    private String hostName;
    private String serviceName;

    /**
     * @param clazzToAddToServerCodebase
     * @param createServerRegistryPort
     */
    public ClientRmiStarter(
        final Class<T> clazzToAddToServerCodebase,
        final Integer  serverRegistryPort,
        final String   hostName,
        final String   serviceName
        )
    {
        super(clazzToAddToServerCodebase, serverRegistryPort);

        this.hostName    = hostName;
        this.serviceName = serviceName;
    }

    /**
     * @param clazzToAddToServerCodebase
     * @param createServerRegistryOnDefautPort
     */
    public ClientRmiStarter(
        final Class<T> clazzToAddToServerCodebase,
        final boolean  serverRegistryPort,
        final String   hostName,
        final String   serviceName
        )
    {
        super(clazzToAddToServerCodebase, serverRegistryPort);

        this.hostName    = hostName;
        this.serviceName = serviceName;
    }

    /**
     * @param clazzToAddToServerCodebase
     */
    public ClientRmiStarter(
        final Class<T> clazzToAddToServerCodebase,
        final String   hostName,
        final String   serviceName
        )
    {
        super(clazzToAddToServerCodebase);

        this.hostName    = hostName;
        this.serviceName = serviceName;
    }

    public T lookup() throws RemoteException, NotBoundException
    {
        Registry registry = LocateRegistry.getRegistry( hostName );

        @SuppressWarnings("unchecked") T remoteObject = (T)registry.lookup( serviceName );

        return remoteObject;
    }

    public String getHostName()
    {
        return hostName;
    }

    public String getServiceName()
    {
        return serviceName;
    }

}
